package com.ewanghuiju.app.presenter.pintuan;

import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.pintuan.PintuanPjContract;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;
import com.muddzdev.styleabletoast.StyleableToast;

import java.util.Map;

import javax.inject.Inject;

import okhttp3.RequestBody;

/**
 * @author sj  qq:751943509
 * @class 拼团评价
 * @time 2020/2/16
 */
public class PintuanPjPresenter extends RxPresenter<PintuanPjContract.View> implements PintuanPjContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public PintuanPjPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getData() {

    }

    @Override
    public void toPingjia(String order_id, String content, String image, String stars) {
        addSubscribe(mDataManager.ptieValuation(order_id, content, image, stars)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if (mView != null) {
                            mView.showPjSuccess();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mView != null) {
                            mView.showPjFail();
                        }
                    }
                })
        );
    }

    @Override
    public void toPingjiaWith(Map<String, RequestBody> files) {
        addSubscribe(mDataManager.uploadFlie(files)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        StyleableToast.makeText(App.getInstance(), "评价成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        if (mView != null) {

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void getOsstoken(String sessionname) {
        addSubscribe(mDataManager.getOsstoken(sessionname)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if (mView != null) {
                            mView.showOsstoken(optional.getIncludeNull());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }
}
